package com.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.domain.User;
import com.service.Impl.UsersServiceImpl;
import com.service.UsersService;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取Session对象
        HttpSession session = request.getSession();
        //获取对象
        UsersService usersService = new UsersServiceImpl();

        //获取用户输入文本框的数据
        String username = request.getParameter("username");
        String passwrod1 = request.getParameter("password1");
        String passwrod2 = request.getParameter("password2");
        String email = request.getParameter("eamil");

        //判断两次输入的密码是否一致
        if(passwrod1.equals(passwrod2)) {
            //判断用户名是否以被注册
            boolean stage = usersService.findName(username);
            if(stage) {
                //用户名存在时
                //将反馈信息存入域中
                session.setAttribute("mess", "温馨提示：该用户名已被注册辣~~");
                //转发到login3.jsp
                request.getRequestDispatcher("page/login3.jsp").forward(request, response);
            }else {
                //不存在时将数据存入数据库
                //1.创建对象将信息封装到User中
                User user = new User();
                user.setUsername(username);
                user.setPassword(passwrod2);
                user.setEmail(email);
                user.setStatus(0);
                //传出对象向
                usersService.addUser(user);
                //转发到login3.jsp
                request.getRequestDispatcher("page/login4.jsp").forward(request, response);
            }
        }else {
            //将反馈信息存入域中
            session.setAttribute("mess", "温馨提示：两次输入的密码不一致~~");
            //转发到login3.jsp
            request.getRequestDispatcher("page/login3.jsp").forward(request, response);
        }
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
